package com.sduept.nwld.wave.util;

import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.utils.CharacterUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ProtectionNwUtil {
	
	public static final String PRIMARY_PROTECTION_1_LABEL = "主一保护";
	public static final String PRIMARY_PROTECTION_2_LABEL = "主二保护";
	public static final String PRIMARY_PROTECTION_3_LABEL = "主三保护";
	
	
	public static void main(String[] args){
		//log.info(getProtectionType("500kV5042开关保护RCS921A"));
		log.info(getProtectionType("500kV铜碧乙线辅助二保护CSC-103AYN-D"));
	}
	
	/**
	 * 从断路器保护中获取断路器名字
	 * @param breakerProtectName
	 * @return
	 */
	public static String getBreakerOfBreakerProtect(String breakerProtectName){
		breakerProtectName = CharacterUtil.convertString(breakerProtectName,CimUtils.break_regex);
		int pos = breakerProtectName.indexOf("断路器");
		Matcher m = CimUtils.BREAKER_NAME_PATTERN.matcher(breakerProtectName);
		if (m.find()) {
			return m.group(0);
		} else if(pos > -1)
			breakerProtectName = CharacterUtil.convertString(breakerProtectName,CimUtils.break_regex);
		return breakerProtectName;
	}
	public static String getProtectionTypeLabel (String protectionName){
		String pType= getProtectionType(protectionName);
		switch (pType) {
		case IProtectionNw.PRIMARY_PROTECTION_1:
			return PRIMARY_PROTECTION_1_LABEL;
		case IProtectionNw.PRIMARY_PROTECTION_2:
			return PRIMARY_PROTECTION_2_LABEL;
		case IProtectionNw.PRIMARY_PROTECTION_3:
			return PRIMARY_PROTECTION_3_LABEL;	
		default:
			return "未知";
		}
	}
	
	public static String getProtectionType(String protectionName){
		Pattern primaryPattern1 = Pattern.compile(IProtectionNw.PRIMARY_PROTECTION_1_KEY);
		Pattern primaryPattern2 = Pattern.compile(IProtectionNw.PRIMARY_PROTECTION_2_KEY);
		Pattern primaryPattern3 = Pattern.compile(IProtectionNw.PRIMARY_PROTECTION_3_KEY);
		Pattern backupPattern1 = Pattern.compile(IProtectionNw.ASSIST_PROTECTION_1_KEY);
		Pattern backupPattern2 = Pattern.compile(IProtectionNw.ASSIST_PROTECTION_2_KEY);
		Pattern breakerPattern1 = Pattern.compile(IProtectionNw.BREAKER_PROTECTION_1_KEY);
		Pattern noElectronnicPattern = Pattern.compile(IProtectionNw.NOELECTRONIC_PROTECTION_KEY);
		Pattern stubPattern = Pattern.compile(IProtectionNw.STUB_PROTECTION_KEY);
		Matcher matcher = null;
		Matcher stubMatcher = stubPattern.matcher(protectionName);
		boolean isStubProtector = false;
		boolean isReactorProtector = false;
		boolean isTransformer = false;
		if(stubMatcher.find()){
			isStubProtector = true;
		}
		
		matcher = noElectronnicPattern.matcher(protectionName);
		if(matcher.find()){
			return IProtectionNw.NOELECTRONIC_PROTECTION;
		}
		matcher = breakerPattern1.matcher(protectionName);
		if(matcher.find()){
			return IProtectionNw.BREAKER_PROTECTION_1;
		}
		if(!NameTool.getTransformerNameOfNw(protectionName).equals(protectionName)){
			isTransformer = true;
		}
		if(!NameTool.getReactorNameOfNw(protectionName).equals(protectionName)||protectionName.contains("电气量")){
			isReactorProtector = true;
		}
		
		if(protectionName.contains("主")||!protectionName.contains("辅") || protectionName.contains("集成")){
			matcher = primaryPattern1.matcher(protectionName);
			if(matcher.find()){
				if(isReactorProtector){
					return IProtectionNw.REACTOR_PROTECTION_1;
				}else if(isStubProtector){
					return IProtectionNw.STUB_PROTECTION_1;
				}else{
					return IProtectionNw.PRIMARY_PROTECTION_1;
				}
				
			}
			
			matcher = primaryPattern2.matcher(protectionName);
			if(matcher.find()){
				if(isReactorProtector){
					return IProtectionNw.REACTOR_PROTECTION_2;
				}else if(isStubProtector){
					return IProtectionNw.STUB_PROTECTION_2;
				}else{
					return IProtectionNw.PRIMARY_PROTECTION_2;
				}
				
			}
			
			matcher = primaryPattern3.matcher(protectionName);
			if(matcher.find()){
				if(isReactorProtector){
					return IProtectionNw.REACTOR_PROTECTION_3;
				}else if(isStubProtector){
					return IProtectionNw.STUB_PROTECTION_3;
				}else if(isTransformer){//变压器保护  保护3 为非电量保护
					return IProtectionNw.NOELECTRONIC_PROTECTION;
				}else{
					return IProtectionNw.PRIMARY_PROTECTION_3;
				}
				
			}
		}
	
		
		
		matcher = backupPattern1.matcher(protectionName);
		if(matcher.find()){
			if(isTransformer){
				return IProtectionNw.NOELECTRONIC_PROTECTION;
			}else{
				return IProtectionNw.ASSIST_PROTECTION_1;
			}
		}
		matcher = backupPattern2.matcher(protectionName);
		if(matcher.find()){
			if(isTransformer){
				return IProtectionNw.NOELECTRONIC_PROTECTION;
			}else{
				return IProtectionNw.ASSIST_PROTECTION_2;
			}
		}
		return protectionName;
	}
	
}
